use serde_json::Value;
use std::collections::HashMap;
use tide::Request;

async fn hello(request: Request<()>) -> tide::Result {
    let name = request.param("name")?.to_string();
    // 组装返回值
    let mut result = HashMap::new();
    result.insert("code", Value::Number(200.into()));
    result.insert("message", Value::String(format!("Hello, {}", name)));
    // 转换成 JSON 格式返回
    let response = tide::Response::builder(200)
        .header("Content-Type", "application/json")
        .body(serde_json::to_string(&result)?)
        .build();
    Ok(response)
}

#[async_std::main]
async fn main() -> tide::Result<()> {
    let mut app = tide::new();
    app.at("/hello/:name").get(hello);
    app.listen("127.0.0.1:18087").await?;
    Ok(())
}
